﻿using System;
using System.Collections.Generic;
using System.Linq;
using Sisocana.Core.Interfaces;
using Sisocana.Core.Models;
using Sisocana.Dal;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal.Interfaces;
using System.Linq.Expressions;

namespace Sisocana.Core
{
    public class NoticiaAsociadoService : ServiceBase<NoticiaAsociadaModel>, INoticiaAsociadaService
    {
        private INoticiaAsociadaRepository _noticiaAsociadaRepository;

        public NoticiaAsociadoService()
            : this(new NoticiaAsociadoRepository())
        {
        }

        public void createNoticiaAsociadaMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<NoticiaAsociado, NoticiaAsociadaModel>()
                .ForMember(s => s.NoticiaNombre, opt => opt.MapFrom(c => c.Noticias.nombre))
                .ForMember(s => s.urlfeed, opt => opt.MapFrom(c => c.Noticias.url))
                .ForMember(s => s.idSitio, opt => opt.MapFrom(c => c.Sitio.id))
                .ForMember(s => s.logoURL, opt => opt.MapFrom(c => c.Noticias.logo.imgNombre));
        }

        public int createNoticiaAsociado(NoticiaAsociadaModel source)
        {

            NoticiaAsociado sourceEF = new NoticiaAsociado();
            sourceEF.cantVisualizar = source.cantVisualizar;
            sourceEF.showDesc = source.showDesc;
            if (source.tagBusqueda == null)
                sourceEF.tagBusqueda = "";
            else
                sourceEF.tagBusqueda = source.tagBusqueda;

            FuenteRepository _fuenteRepository = new FuenteRepository();
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();
            IEnumerable<Noticias> listsourcesEF = fuentesEF.OfType<Noticias>();

            Noticias newsEF = listsourcesEF.AsQueryable<Noticias>().Where(c => c.nombre.Trim() == source.NoticiaNombre.Trim()).First();


            SiteRepository _SiteRepository = new SiteRepository();
            Sitio sitEF = _SiteRepository.GetSingle(c => c.id == source.idSitio);


            sourceEF.Sitio = sitEF;
            sourceEF.Noticias = newsEF;

            _noticiaAsociadaRepository.Add(sourceEF);

            try
            {
                _noticiaAsociadaRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public int updateNoticiaAsociado(NoticiaAsociadaModel source)
        {
            //busco la asociacion 
            NoticiaAsociado sourceEF = _noticiaAsociadaRepository.GetSingle(c => c.id == source.id);
            sourceEF.cantVisualizar = source.cantVisualizar;
            sourceEF.showDesc = source.showDesc;
            if (source.tagBusqueda == null)
                sourceEF.tagBusqueda = "";
            else
                sourceEF.tagBusqueda = source.tagBusqueda;
            try
            {
                _noticiaAsociadaRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }

        public NoticiaAsociadoService(INoticiaAsociadaRepository noticiaAsociadaRepository)
        {
            _noticiaAsociadaRepository = noticiaAsociadaRepository;
        }

        public NoticiaAsociadaModel GetSingle(System.Linq.Expressions.Expression<Func<NoticiaAsociadaModel, bool>> whereCondition)
        {
            createNoticiaAsociadaMapperEF2Model();

            IList<NoticiaAsociado> sourcesEF = _noticiaAsociadaRepository.GetAll();
            List<NoticiaAsociadaModel> sources = new List<NoticiaAsociadaModel>();

            sources = AutoMapper.Mapper.Map(sourcesEF, sources);

            IQueryable<NoticiaAsociadaModel> sourceRet = sources.AsQueryable<NoticiaAsociadaModel>().Where(whereCondition);

            List<NoticiaAsociadaModel> ret = sourceRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }

        public int deleteNoticiaAsociado(NoticiaAsociadaModel nam)
        {
            NoticiaAsociado na = _noticiaAsociadaRepository.GetSingle(c => c.id == nam.id);

            if (na == null)
                return -2;

            try
            {
                _noticiaAsociadaRepository.Delete(na);
                _noticiaAsociadaRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
        }

        void IService<NoticiaAsociadaModel>.Add(NoticiaAsociadaModel entity)
        {
            throw new NotImplementedException();
        }

        void IService<NoticiaAsociadaModel>.Delete(NoticiaAsociadaModel entity)
        {
            throw new NotImplementedException();
        }

        void IService<NoticiaAsociadaModel>.Update(NoticiaAsociadaModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<NoticiaAsociadaModel> GetAll(Expression<Func<NoticiaAsociadaModel, bool>> whereCondition)
        {
            createNoticiaAsociadaMapperEF2Model();

            IList<NoticiaAsociado> sourceEF = _noticiaAsociadaRepository.GetAll();
            List<NoticiaAsociadaModel> sources = new List<NoticiaAsociadaModel>();
            sources = AutoMapper.Mapper.Map(sourceEF, sources);
            IQueryable<NoticiaAsociadaModel> sRet = sources.AsQueryable<NoticiaAsociadaModel>().Where(whereCondition);
            return sRet.ToList();
        }

        IList<NoticiaAsociadaModel> IService<NoticiaAsociadaModel>.GetAll()
        {
            throw new NotImplementedException();
        }

        IQueryable<NoticiaAsociadaModel> IService<NoticiaAsociadaModel>.Query(System.Linq.Expressions.Expression<Func<NoticiaAsociadaModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        long IService<NoticiaAsociadaModel>.Count(System.Linq.Expressions.Expression<Func<NoticiaAsociadaModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        long IService<NoticiaAsociadaModel>.Count()
        {
            throw new NotImplementedException();
        }
    }
}
